package org.example.channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 读文件
 */
public class Demo2 {
    public static void main(String[] args) throws IOException {
        //创建文件输入流
        File file = new File("demo.txt");
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            //获取文件通道
            FileChannel channel = fileInputStream.getChannel();
            //创建缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
            //读取通道
            int read = channel.read(byteBuffer);
            //输入读取信息
            System.out.println(read);
            System.out.println(new String(byteBuffer.array()));
        }
    }
}
